package com.util;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class excelUtil {

    /**
     * Reading cell value
     * @param cell
     * @return
     */
    public static String getCellStrValue(XSSFCell cell) {
        String cellValue = "";
        if(cell != null) {
            switch (cell.getCellTypeEnum()) {
                case STRING: {
                    cellValue = cell.getRichStringCellValue().getString();
                    break;
                }
                case NUMERIC: {
                    if (DateUtil.isCellDateFormatted(cell)) {
                        Date date = cell.getDateCellValue();
                        cellValue = DateFormatUtils.format(date, "yyyy-MM-dd");
                    } else {
                        cell.setCellType(CellType.STRING);
                        cellValue = String.valueOf(cell.getRichStringCellValue().getString());
                    }
                    break;
                }
                case BLANK: {
                    cellValue = "null";
                    break;
                }
                default:
                    cellValue = "null";
                    break;
            }
        }
        else {
            cellValue = "null";         //空白读取报空指针
        }
        return cellValue;
    }

    /**
     * Getting pic and index
     * @param sheet
     * @return Map<PicIndex, PicData>
     */
    public static Map<String, PictureData> getSheetPictures(XSSFSheet sheet) {
        Map<String, PictureData> index_pic = new HashMap<>();
        for(POIXMLDocumentPart dr: sheet.getRelations()) {
            if(dr instanceof XSSFDrawing) {
                XSSFDrawing drawing = (XSSFDrawing)dr;
                List<XSSFShape> shapes = drawing.getShapes();
                for(XSSFShape shape: shapes) {
                    if(!(shape instanceof XSSFShapeGroup)) {
                        XSSFPicture pic = (XSSFPicture) shape;
                        XSSFAnchor anchor = pic.getAnchor();
                        if (anchor instanceof XSSFClientAnchor) {
                            //SheetNum_RowNum_ColNum
                            String picIndex = ((XSSFClientAnchor)anchor).getRow1() + "_" + ((XSSFClientAnchor)anchor).getCol1();
                            index_pic.put(picIndex, pic.getPictureData());
                        }
                    }
                }
            }
        }
        return index_pic;
    }

//
//    public static Map<String, String> saveExcelPic(String path, Map<String, PictureData> index_pic) throws IOException {
//        Map<String, String> index_fileName = new HashMap<>();
//        for(Map.Entry<String, PictureData> entry: index_pic.entrySet()) {
//            PictureData pic = entry.getValue();
//            String picName = entry.getKey();
//            String ext = pic.suggestFileExtension(); //格式获取
//
//            String final_path = path + picName + "." + ext;
//            byte[] data = pic.getData();
//            FileOutputStream out = new FileOutputStream(new File(final_path));
//            out.write(data);
//            out.close();
//        }
//        return index_fileName;
//    }

}
